//
//  InSickerViewController.m
//  HonlivApp
//
//  Created by admin on 2017/6/25.
//  Copyright © 2017年 honliv. All rights reserved.
//

#import "InSickerViewController.h"
#import "InSickerViewModel.h"
#import "InPatientModel.h"
#import "InpatientCell.h"

@interface InSickerViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *datasArray;
@property (nonatomic,assign) NSInteger pageNum;

@end

@implementation InSickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"住院患者";
    [self initTableView];
    [_tableView.mj_header beginRefreshing];
}

-(void)initTableView{
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-64) style:UITableViewStyleGrouped];
//    _tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 130;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    [self createRefresh];
}

//加载数据
-(void)getDatasArray{
    //{db:"OracleString",SqlNo:"21",主管医生工号:"0802",Page:"1"}
    //{db:"OracleString",SqlNo:"24",科室编码:"010713",主管医生工号:"0802",Page:"1"}
    InSickerViewModel *viewModel = [[InSickerViewModel alloc] initWithType:self.type doctor:[User currentUser].username department:[User currentUser].department page:_pageNum];
    [viewModel getInSickerInfoSuccess:^(NSArray *array) {
        [_datasArray addObjectsFromArray:array];
        if (_datasArray.count == 0) {
            [SVProgressHUD showErrorWithStatus:@"暂无更多数据..."];
            [_tableView.mj_footer endRefreshingWithNoMoreData];;
        }else if (array.count == 0) {
            [_tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self endRefersh];
            [_tableView reloadData];
        }

    }];
}
//上拉加载。下拉刷新
-(void)createRefresh{
    _datasArray = [NSMutableArray array];
    _tableView.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        _pageNum = 1;
        [_datasArray removeAllObjects];
        [self getDatasArray];
    }];
    _tableView.mj_footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        _pageNum ++;
        [self getDatasArray];
    }];
    
}

//结束刷新
-(void)endRefersh{
    [_tableView.mj_header endRefreshing];
    [_tableView.mj_footer endRefreshing];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _datasArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    InPatientModel *model = _datasArray[section];
    NSString *title = model.date;
    return [title substringToIndex:title.length - 8];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    InpatientCell *cell = [InpatientCell cellWithTableView:tableView];
    InPatientModel *model = _datasArray[indexPath.section];
    cell.inpatient = model;
    return cell;
}

@end
